<template>
	<view class="uniLogin" >
		<image src="../static/images/loginbg_01.png" mode="" class="bgImage" ></image>
		<view class="loginBox"  :style="{paddingTop: statusBarHeight + 'px'}">
			<view class="loginSize f18">登录</view>
			<view class="loginCenter boxShow">
				<view class="line">
					<input type="text" value="" placeholder="用户名" v-model="loginForm.userName"/>
				</view>
				<view class="line">
					<input type="text" password="true" value="" placeholder="密  码" v-model="loginForm.password"/>
				</view>
				<view class="line flex flex-between align-center">
					<input type="text" value="" placeholder="请输入验证码" v-model="loginForm.verification"/>
					<image :src="'data:image/jpeg;base64,' +uimg" mode="" class="capImage" @click="getImage"></image>
				</view>
				<view class="loginBtn" @click="chackCap">登录</view>
				<view class="canal" @click="canal">取消</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {getCapchImage,setChcaph,upLogin,getUserInfor,getcheckList} from '../util/api.js'
	export default {
		data() {
			return {
				statusBarHeight: uni.getSystemInfoSync().statusBarHeight,
				uimg:'',
				uuid:'',
				loginForm: {
					userName: 'jgjxy',   //lisi 核实人员  wangwu 核查人员  zhangsan admin  jgjxy机构接线员 jgjxy2
					password: '123456',
					verification:'',
				},
			}
		},
		onLoad() {
			uni.removeStorageSync('token');
			uni.removeStorageSync('token_type');
			uni.removeStorageSync('userInfor');
			uni.removeStorageSync('roles');
			this.getImage();
		},
		mounted() {
			const userName = uni.getStorageSync('userName');
			const password = uni.getStorageSync('password');
			if (userName && password) {
				this.loginForm.userName = userName;
				this.loginForm.password = password;
			} else {
				this.loginForm.userName = '';
				this.loginForm.password = '';
			}
		},
		methods: {
			canal(){
				// uni.navigateTo({
				// 	url:'/pages/map/allmarkMap'
				// })
			},
			getImage(){
				getCapchImage().then((res)=>{
					if(res.code == 200){
						this.uimg = res.img
						this.uuid = res.uuid;
					}else{
						this.$toast.showToast(res.msg);
					}
				}).catch((err)=>{
					console.log(err);
				})
			},
			//效验验证码
			chackCap(){
				if(this.loginForm.verification == ''){
					this.$toast.showToast('请输入验证码');
					return;
				}
				var parme={
					code:this.loginForm.verification,
					uuid:this.uuid
				}
				setChcaph(parme).then((res)=>{
					if(res.code == 200){
						this.loginIn()
					}else{
						this.$toast.showToast(res.msg);
						this.getImage();
					}
				}).catch((err)=>{
					console.log(err);
					if(err.code == 500){
						this.$toast.showToast(err.msg)
						this.getImage();
					}
				})
			},
			//获取token
			loginIn(){
				if(!this.loginForm.userName){
					this.$toast.showToast('请填写用户名');
					return;
				}
				if(!this.loginForm.password){
					this.$toast.showToast('请填写密码');
					return;
				}
				uni.showLoading({
					title: "正在登录",
				    mask: true,
				});
				// this.loginForm.pushId = uni.getStorageSync('pushId');
				var parmse = {
					username:this.loginForm.userName,
					password:this.loginForm.password,
					grant_type: "password" 
				}
				upLogin(parmse).then((res)=>{
					uni.hideLoading()
					uni.setStorageSync('token', res.access_token);
					uni.setStorageSync('token_type', res.token_type);
					this.getinfor()
				}).catch((err)=>{
					if(err.error){
						this.$toast.showToast(err.error +"错误"+ err.error_description)
					}
				})
			},
			//获取用户信息 
			getinfor(){
				var parse = {
					pushId:uni.getStorageSync('pushId')?uni.getStorageSync('pushId'):'0adcfeb7e4d55164ac5f5b2d1fb4b0d8'
				}
				getUserInfor(parse).then((res)=>{
					if(res.code == 200){
						uni.setStorageSync('userInfor',res.user),
						uni.setStorageSync('szcgArchivesSrc',res.szcgArchivesSrc),
						uni.setStorageSync('roles',res.roles)
						uni.setStorageSync('userName', this.loginForm.userName);
						uni.setStorageSync('password', this.loginForm.password);
						//判断是不是包含角色的
						if(this.$checkPermission(['hc_personnel','hs_personnel','xc_personnel','cz_personnel','bdjgjxy','supervisor']) == true){
							this.$toast.showToast('登录成功');
							uni.switchTab({
								url:'/pages/index/index'
							})
							this.checkUser()
						}else{
							this.$toast.showToast('没有权限登录');
						}
						
					}else{
						this.$toast.showToast('登录失败')
					}
				}).catch((err)=>{
					console.log(err)
				})
			},
			//判断巡查人员是否开启定时器
			checkUser(){
				/////判断是否是巡查人员如果是巡查人员 判断最后一次打卡是上班还是下班如果是上班打开就开启定时器
				if(this.$checkPermission(['xc_personnel']) == true){
					getcheckList({},uni.getStorageSync('userInfor').userId).then((res)=>{
						if(res.code == 200){
							if(res.data){
								if(res.data.type == 1){
									uni.$emit('startLocationTimer')
								}else if(res.data.type == 2){
									uni.$emit('killLocationTimer')
								}
							}else{
								console.log('空')
							}
						}else{
							this.$toast.showToast('获取统计失败')
						}
					}).catch((err)=>{})
				}
			},
		}
	}
</script>

<style scoped lang="scss">
	.uniLogin{
		.bgImage{
			width: 100%;
		}
		.loginBox{
			position: absolute;
			width: 100%;
			top: 100rpx;
			.loginSize{
				color: #FFFFFF;
				margin-left: 100rpx;
			}
			.loginCenter{
				width: 680rpx;
				margin: 0 auto;
				margin-top: 70rpx;
				background-color: #FFFFFF;
				border-radius: 40rpx;
				box-sizing: border-box;
				padding: 100rpx 40rpx 170rpx 40rpx;
				.line{
					padding: 20rpx;
					border-bottom: 1px solid #E2E2E2;
					input{
						padding: 20rpx 0rpx 20rpx 0rpx;
					}
					.capImage{
						width: 180rpx;
						height: 80rpx;
					}
				}
				.loginBtn{
					margin-top: 100rpx;
					margin-bottom: 35rpx;
					background-image: linear-gradient(to right, #44B1EE, #3A8FED);
					color: #FFFFFF;
					padding: 35rpx 0rpx 35rpx 0rpx;
					border-radius: 100rpx;
					text-align: center;
					font-size: 34rpx;
				}
				.canal{
					border: 1px solid #EEEEEE;
					padding: 35rpx 0rpx 35rpx 0rpx;
					border-radius: 100rpx;
					text-align: center;
					font-size: 34rpx;
					color: #6F6F6F;
				}
			}
			
		}
	}
</style>
